'use strict'
import {
  Model,
  DataTypes,
  InferAttributes,
  InferCreationAttributes,
  CreationOptional,
} from 'sequelize'
import dayjs from 'dayjs'
import { Models } from '../type'
import sequelize from '.'

class Brand extends Model<InferAttributes<Brand>, InferCreationAttributes<Brand>> {
  declare id: CreationOptional<number>
  declare brandName: string
  declare brandLogo: string
  declare createdAt: CreationOptional<Date>
  declare updatedAt: CreationOptional<Date>

  static associate(models: Models) {}
}
Brand.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
    },
    brandName: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {
          msg: '品牌名称必须填写。',
        },
        notEmpty: {
          msg: '品牌名称不能为空。',
        },
        len: {
          args: [2, 45],
          msg: '品牌名称长度必须在2~45个字符之间。',
        },
      },
    },
    brandLogo: {
      type: DataTypes.STRING,
      allowNull: false,
      validate: {
        notNull: {
          msg: '品牌LOGO必须填写。',
        },
        notEmpty: {
          msg: '品牌LOGO不能为空。',
        },
        isUrl: {
          msg: '品牌LOGO地址不正确。',
        },
      },
    },
    createdAt: {
      type: DataTypes.DATE,
      get() {
        return this.getDataValue('createdAt')
          ? dayjs(this.getDataValue('createdAt')).format('YYYY-MM-DD HH:mm:ss')
          : null
      },
    },
    updatedAt: {
      type: DataTypes.DATE,
      get() {
        return this.getDataValue('updatedAt')
          ? dayjs(this.getDataValue('updatedAt')).format('YYYY-MM-DD HH:mm:ss')
          : null
      },
    },
  },
  {
    sequelize,
    modelName: 'Brand',
  },
)

export default Brand
